import axios from 'axios'
export default {
	state() {
		return {
			list: []
		}
	},
	mutations: {
		changeList(state,list){
			state.list = list
		}
	},
	actions: {
		async getList({commit}){
			const res  =await axios('http://localhost:3000/cart')
			commit('changeList',res.data)
		},
		
		async countList({commit},obj){
			const res  =await axios.patch('http://localhost:3000/cart/'+obj.id,{
				count:obj.count
			})
		}
	},
	getters: {
		total(state) {
		  return state.list.reduce((p, c) => p + c.count, 0);
		},
		totalPrice (state) {
		  return state.list.reduce((p, c) => p + c.count * c.price, 0);
		},
	  },
	namespaced: true
}
